/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hugoo.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Lovelock Chan（LoL）
 */
public class T4 {

    private static final String verify18PatternStr = "(\\d{6})" + "(\\d{8})" + "(\\d{3})" + "([\\d[xX]]{1})";
    private static final Pattern verify18Pattern = Pattern.compile(verify18PatternStr);
    private static final String verify15PatternStr = "(\\d{6})" + "(\\d{6})" + "(\\d{3})";
    private static final Pattern verify15Pattern = Pattern.compile(verify15PatternStr);
    private static final int verify18Rights[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
    private static final String verify18CheckDigit[] = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
    private String areaNum;
    private String birthdayNum;
    private String seqNum;
    private String check_digit;
    private int length;
    public static void main(String[] args) {
        System.out.println("yt3e".substring(2, 3));
        System.out.println("ytye".matches("^[yYnN][tT][yYnN][eE]$"));
    }
    public boolean isMan() {

        int num = Integer.parseInt(seqNum);

        if (num % 2 == 1) {

            return true;

        } else {
            return false;
        }

    }

    public String getBirthday() {

        String FullBirthdayNum = null;

        if (length == 15)//在生日号码前加“19”
        {
            FullBirthdayNum = "19" + birthdayNum;
        } else {
            FullBirthdayNum = birthdayNum;
        }

        String year = FullBirthdayNum.substring(0, 4);
        String month = FullBirthdayNum.substring(4, 6);
        String date = FullBirthdayNum.substring(6, 8);

        return year + "-" + month + "-" + date;

    }

    public void Verifier(String IDCardNO) throws LengthIllegalException, CheckDigitInvalidException, NumberInvalidException {

        if (IDCardNO == null) {
            throw new NumberInvalidException();
        }

        length = IDCardNO.length();
        switch (length) {
            case 15:
                Verifier15(IDCardNO);
                break;
            case 18:
                Verifier18(IDCardNO);
                break;
            default:
                throw new LengthIllegalException();
        }
    }

    private void Verifier15(String IDCardNO) throws NumberInvalidException {
        Matcher m = verify15Pattern.matcher(IDCardNO);
        if (m.matches() == false) {
            throw new NumberInvalidException();
        }
        areaNum = m.group(1);
        birthdayNum = m.group(2);
        seqNum = m.group(3);
    }

    private void Verifier18(String IDCardNO) throws CheckDigitInvalidException, NumberInvalidException {
        Matcher m = verify18Pattern.matcher(IDCardNO);

        if (m.matches() == false) {

            throw new NumberInvalidException();

        }

        areaNum = m.group(1);
        birthdayNum = m.group(2);
        seqNum = m.group(3);
        check_digit = m.group(4);

        //预期的校验位:
        String expect_check_digit = getCheck_digit18(IDCardNO);

        //如果校验位无效
        if (expect_check_digit.equalsIgnoreCase(check_digit) == false) {
            throw new CheckDigitInvalidException();
        }

    }

    /**
     * 1、将前面的身份证号码17位数分别乘以不同的系数。第i位对应的数为[2^(18-i)]mod11。从第一位到第十七位的系数分别为：7 9 10 5
     * 8 4 2 1 6 3 7 9 10 5 8 4 2 ； 2、将这17位数字和系数相乘的结果相加； 3、用加出来和除以11，看余数是多少？；
     *
     * @param IDCardNO
     * @return
     */
    public static String getCheck_digit18(String IDCardNO) {

        //权总值
        int sum = 0;
        for (int i = 0; i <= 16; i++) {
            int num = Integer.parseInt(IDCardNO.substring(i, i + 1));
            int right = verify18Rights[i];
            sum = sum + num * right;
        }

        //对权总值取模
        int y = sum % 11;
        return verify18CheckDigit[y];
    }

    public static String IDCardNO15To18(String IDCardNO15) throws
            NumberInvalidException {



        Matcher m = verify15Pattern.matcher(IDCardNO15);

        if (m.matches() == false) {
            throw new NumberInvalidException();
        }



        String NO17 = IDCardNO15.substring(0, 6) + "19" + IDCardNO15.substring(6, 15);

        return NO17 + getCheck_digit18(NO17);

    }

    public String getAreaNum() {

        return areaNum;

    }

    public String getBirthdayNum() {

        return birthdayNum;

    }

    public String getCheck_digitNum18() {

        return check_digit;

    }

    public int getLength() {

        return length;

    }

    public String getSeqNum() {

        return seqNum;

    }

    public static class NumberInvalidException extends Exception {

        public NumberInvalidException() {
        }
    }

    public static class CheckDigitInvalidException extends Exception {

        public CheckDigitInvalidException() {
        }
    }

    public static class LengthIllegalException extends Exception {

        public LengthIllegalException() {
        }
    }
}
